#include <stdio.h>
#include <stdlib.h>
#include <cstring>


void work(char *infile, char *outfile){
    FILE *fi,*fo,*foi;
    char name[105];
    int i;
    bool flag;
    fi=fopen(infile,"r");
    fo=fopen(outfile,"w");
    foi=fopen("ifile.lst","w");
    if (fi==NULL||fo==NULL||foi==NULL){
	printf("failed to open file\n");
	exit(1);
    }
    i=0;
    while (fscanf(fi,"%s",name)!=EOF){
	flag=0;
	if (strstr(name,"bigWig")!=NULL||strstr(name,"bw")!=NULL){
	    fprintf(fo,"py ../gis-basic-storage/console/bcs.py build bigWig -i /home/ruijie/gbdb/hg19/bbi/%s -F bigWig --importmode=softlink --tags=assembly=hg19 --queryop='max,access'\n",name);
	    flag=1;
	}
	/*if (strstr(name,"bigBed")!=NULL||strstr(name,"bb")!=NULL){
	    fprintf(fo,"py ../gis-basic-storage/console/bcs.py build bigBed -i /home/ruijie/gbdb/hg19/bbi/%s -F bigBed --importmode=softlink --tags=assembly=hg19 --queryop='max,access'\n",name);
	    flag=1;
	}*/
	if (flag){
	    fprintf(foi,"%s\n",strtok(name,"."));
	    i++;
	}
	//if (i>3) break;
    }
    fclose(fi);
    fclose(fo);
    fclose(foi);
}

void exit_with_help(){
    printf("Usage: genImport fileList [outfile]\nIf outfile is not provided, \"import.sh\" will be used\n");
    exit(1);
}

int main(int argc, char **argv){
    char outfile[50]="import.sh";
    if (argc<2)
	exit_with_help();
    if (argc>=3)
	strcpy(outfile,argv[2]);
    work(argv[1],outfile);
    return 0;
}
